package com.prawn.ift_baseconfig.service.impl;


import com.prawn.ift_baseconfig.dao.NetworkDao;
import com.prawn.ift_baseconfig.entity.Equipment;
import com.prawn.ift_baseconfig.pojo.Manufactor;
import com.prawn.ift_baseconfig.pojo.Network;
import com.prawn.ift_baseconfig.service.EquipmentClient;
import com.prawn.ift_baseconfig.service.NetWorkService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 网络信息服务实现
 */
@Service
public class NetworkServiceImpl implements NetWorkService {

    @Autowired
    private NetworkDao networkDao;

    /**
     * 添加网络信息
     *
     * @param network
     * @return Result
     */
    @Override
    public Result addNetWork(Network network) {
        if (networkDao.save(network) != null) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 删除网络信息
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteNetWork(String id) {
        networkDao.deleteById(id);
        return Result.success();
    }

    /**
     * 编辑网络信息
     *
     * @param network
     * @return Result
     */
    @Override
    public Result editNetWork(Network network) {

        Network oldNetwork =networkDao.findById(network.getId()).get();
        BeanUtils.copyProperties(network,oldNetwork, NullAttributeUtils.getNullPropertyNames(network));


        networkDao.save(oldNetwork) ;
            return Result.success();

    }

    /**
     * 查询网络信息
     *
     * @param id
     * @return Network
     */
    @Override
    public Network getNetWorkById(String id) {
        return networkDao.findById(id).get();
    }

    /**
     * 通过设备id查询网络信息
     *
     * @param id
     * @return Network
     */
//    @Autowired
//    private EquipmentClient equipmentClient;

    @Override
    public Result getNetWorkByequipmentId(String id) {

//        String r = equipmentClient.getEquimentById(id);
//        System.out.println(r);


        Network network = new Network();
        network.setEquipmentId(id);
        Example<Network> sExample = Example.of(network);
        List<Network> networkList = networkDao.findAll(sExample);


        return Result.success().add(networkList);
    }

    /**
     * 查询网络信息列表
     *
     * @return Result
     */
    @Override
    public Result getNetWorkList() {

        List<Network> networkList = networkDao.findAll();
        return Result.success().add(networkList);
    }

    /**
     * 分页查询网络信息列表
     *
     * @return Result
     */
    @Override
    public Result getNetWorkPage(int page, int rows) {
        PageRequest pageRequest = PageRequest.of(page - 1, rows);
        Page<Network> p = networkDao.findAll(pageRequest);
        PageResult<Network> pageResult = new PageResult<>(p.getTotalElements(), p.getContent());
        return Result.success().add(pageResult);
    }


    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Network> createSpecification(Map searchMap) {

        return new Specification<Network>() {

            @Override
            public Predicate toPredicate(Root<Network> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                    predicateList.add(cb.like(root.get("id").as(String.class), "%" + (String) searchMap.get("id") + "%"));
                }
                // 网络地址
                if (searchMap.get("location") != null && !"".equals(searchMap.get("location"))) {
                    predicateList.add(cb.like(root.get("location").as(String.class), "%" + (String) searchMap.get("location") + "%"));
                }
                // 引用网络类型id
                if (searchMap.get("networkTypeId") != null && !"".equals(searchMap.get("networkTypeId"))) {
                    predicateList.add(cb.like(root.get("networkTypeId").as(String.class), "%" + (String) searchMap.get("networkTypeId") + "%"));
                }
                // 引用设备id
                if (searchMap.get("equipmentId") != null && !"".equals(searchMap.get("equipmentId"))) {
                    predicateList.add(cb.like(root.get("equipmentId").as(String.class), "%" + (String) searchMap.get("equipmentId") + "%"));
                }
                // 连接方式，数据字典
                if (searchMap.get("linkType") != null && !"".equals(searchMap.get("linkType"))) {
                    predicateList.add(cb.like(root.get("linkType").as(String.class), "%" + (String) searchMap.get("linkType") + "%"));
                }
                // SIM 卡号
                if (searchMap.get("simCard") != null && !"".equals(searchMap.get("simCard"))) {
                    predicateList.add(cb.like(root.get("simCard").as(String.class), "%" + (String) searchMap.get("simCard") + "%"));
                }
                // 短信中心号
                if (searchMap.get("smsNo") != null && !"".equals(searchMap.get("smsNo"))) {
                    predicateList.add(cb.like(root.get("smsNo").as(String.class), "%" + (String) searchMap.get("smsNo") + "%"));
                }
                // 应用类型，数据字典
                if (searchMap.get("applicationType") != null && !"".equals(searchMap.get("applicationType"))) {
                    predicateList.add(cb.like(root.get("applicationType").as(String.class), "%" + (String) searchMap.get("applicationType") + "%"));
                }
                // 协议类型，数据字典
                if (searchMap.get("protocolType") != null && !"".equals(searchMap.get("protocolType"))) {
                    predicateList.add(cb.like(root.get("protocolType").as(String.class), "%" + (String) searchMap.get("protocolType") + "%"));
                }
                // 备注信息
                if (searchMap.get("remarks") != null && !"".equals(searchMap.get("remarks"))) {
                    predicateList.add(cb.like(root.get("remarks").as(String.class), "%" + (String) searchMap.get("remarks") + "%"));
                }
                // 创建者
                if (searchMap.get("createBy") != null && !"".equals(searchMap.get("createBy"))) {
                    predicateList.add(cb.like(root.get("createBy").as(String.class), "%" + (String) searchMap.get("createBy") + "%"));
                }
                // 更新者
                if (searchMap.get("updateBy") != null && !"".equals(searchMap.get("updateBy"))) {
                    predicateList.add(cb.like(root.get("updateBy").as(String.class), "%" + (String) searchMap.get("updateBy") + "%"));
                }
                // 删除标记
                if (searchMap.get("delFlag") != null && !"".equals(searchMap.get("delFlag"))) {
                    predicateList.add(cb.like(root.get("delFlag").as(String.class), "%" + (String) searchMap.get("delFlag") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }
}
